# **************************************************************************
# Makefile
#
# Makefile for creating MS Windows installers for the simage binary SDK for
# MS Windows.
#
# Prerequisites:
#   Cygwin system.
#   makensis - the nullsoft installer system
#
# Authors:
#   Lars J. Aas <larsa@sim.no>
#   Thomas Hammer <thammer@sim.no>
# **************************************************************************

project = simage
Project = simage

major = @SIMAGE_MAJOR_VERSION@

sdk_prefix = /tmp/coin3d/$(project)
top_srcdir = @simage_src_dir@

# default c library option
libc = md

ALLCFLAGS = /WX /noBool
DBGCFLAGS = $(ALLCFLAGS) /GZ /Zi /Yd
OPTCFLAGS = $(ALLCFLAGS) /Oitgb1 /Gs
DLLCFLAGS = /GD

CONFIGUREOPTIONS =
BUILDOPTIONS = --disable-dependency-tracking

BUILDS = buildfiles shared-release shared-debug # static-release static-debug

# **************************************************************************

all:: $(BUILDS) $(project)-v$(VERSION)-install.exe

# **************************************************************************

unix2dos.sed: Makefile
	@echo -e "s/\$$/\r/;\np;" >unix2dos.sed

LICENSE.TXT:
	@cp $(top_srcdir)/COPYING LICENSE.TXT;

# FIXME: use full makensis path (and support spaces in the path)...
# something like ac_subst $(sim_ac_makensis_exe)

# FIXME: add simage-sdk.html. 2003-11-19 thammer.

$(project)-v$(VERSION)-install.exe: $(project).nsi LICENSE.TXT
	makensis $(project).nsi

$(Project)-$(VERSION): $(Project)-$(VERSION).tar.gz
	tar zxf $(Project)-$(VERSION).tar.gz

# Note: The processing of Makefile below is done because the dist
# target in Makefile.in, generated by automake, is incompatible with
# filesystems mounted in textmode under cygwin. 2003-11-18 thammer.

$(Project)-$(VERSION).tar.gz:
	@( cd ..; \
	  cp Makefile Makefile.org; \
cat Makefile.org | sed -e 's/$$(AMTAR) chof - $$(distdir) | GZIP=$$(GZIP_ENV) gzip -c >$$(distdir).tar.gz/GZIP=$$(GZIP_ENV) $$(AMTAR) czhof $$(distdir).tar.gz $$(distdir)/g' > Makefile; \
           $(MAKE) dist; mv $(Project)-$(VERSION).tar.gz windows/; \
        )

headerlist.nsi: $(project)_include.txt Makefile
	winpath=`CYGWIN= cygpath -w "$(sdk_prefix)"`; \
	( prevdir=; for path in `cat $(project)_include.txt`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g'`; \
	    echo "SetOutPath \$$INSTDIR\\$$dir"; \
	    echo "File $$winpath\\$$dir\\*.h"; \
	  fi; \
	done ) >headerlist.nsi

$(project)_include.txt: Makefile
	(cd $(sdk_prefix); \
	find include -type f -name *.h) \
	  > $(project)_include.txt

sourcelist.nsi: $(project)_source.txt Makefile
	( prevdir=""; for path in `cat $(project)_source.txt`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g'`; \
	    echo "SetOutPath \$$INSTDIR\\source\\$$dir"; \
	    echo "File $$dir\\*"; \
	  fi; \
	done ) >sourcelist.nsi

$(project)_source.txt: $(Project)-$(VERSION) Makefile
	find $(Project)-$(VERSION) -type f \
	  > $(project)_source.txt

buildfileslist.nsi: $(project)_build.txt Makefile
	( prevdir=; for path in `cat $(project)_build.txt`; do \
	  dir=`echo $$path | sed 's/\/[^\/]*$$//'`; \
	  if test x"$$dir" != x"$$prevdir"; then \
	    prevdir=$$dir; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    dir=`echo $$dir | sed 's/\//\\\\/g' | cut -c2-`; \
	    echo "SetOutPath \$$INSTDIR\\build$$dir"; \
	    echo "File .\\build-files$$dir\\$$file"; \
	  else \
	    dir=`echo $$dir | sed 's/\//\\\\/g' | cut -c2-`; \
	    file=`echo $$path | sed 's/^.*\///g'`; \
	    echo "File .\\build-files$$dir\\$$file"; \
	  fi; \
	done ) >buildfileslist.nsi

$(project)_build.txt: buildfiles
	(cd build-files; \
	echo "./$(project)$(major).dsp"; \
	echo "./$(project)$(major).dsw"; \
	echo "./config.h" ) > $(project)_build.txt

uninstall.nsi: Makefile
	( cd $(sdk_prefix); \
	for file in `find . -type f -depth`; do \
	  file=`echo $$file | sed 's/^.\///; s/\//\\\\/g'`; \
	  echo "Delete \$$INSTDIR\\$$file"; \
	done; \
	for dir in `find . -type d -depth`; do \
	  dir=`echo $$dir | sed 's/^.\///; s/\//\\\\/g'`; \
	  echo "RMDir \$$INSTDIR\\$$dir"; \
	done ) >uninstall.nsi

$(project).nsi: heading.nsi headerlist.nsi middle.nsi sourcelist.nsi bfheader.nsi buildfileslist.nsi post.nsi uninstall.nsi footer.nsi unix2dos.sed Makefile
	cat heading.nsi headerlist.nsi middle.nsi sourcelist.nsi bfheader.nsi buildfileslist.nsi post.nsi uninstall.nsi footer.nsi >$(project).tmp
	winpath=`CYGWIN= cygpath -w "$(sdk_prefix)"`; \
	winpath=`echo $$winpath | sed -e 's/\\\\/\\\\\\\\/g'`; \
	sed -e 's/@win_prefix@/'$$winpath'/g' \
	    -e 's/@sdk_version@/$(VERSION)/g' <$(project).tmp >$(project).tmp2
	sed -n -f unix2dos.sed <$(project).tmp2 >$(project).nsi
	rm -f $(project).tmp $(project).tmp2

# **************************************************************************

3rdparty :
	@if test -d 3rdparty; then :; else mkdir 3rdparty; fi
	builddir=`pwd`; \
	cd $(top_srcdir)/build; \
	MAKEFLAGS=""; \
	/usr/bin/bash $(top_srcdir)/build/build_simage_libs_win32.sh $$builddir/3rdparty

build-shared-release/src/Inventor/Win/$(project)$(major).dll: 3rdparty $(Project)-$(VERSION)
	@if test -d build-shared-release; then :; else mkdir build-shared-release; fi
	builddir=`pwd`; \
	cd build-shared-release; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(OPTCFLAGS) $(DLLCFLAGS)" \
	  CXXFLAGS="$(OPTCFLAGS) $(DLLCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --with-ungif="$$builddir/3rdparty/dist/libungif/$(libc)" \
	  --with-jpeg="$$builddir/3rdparty/dist/libjpeg/$(libc)" \
	  --with-zlib="$$builddir/3rdparty/dist/zlib/$(libc)" \
	  --with-png="$$builddir/3rdparty/dist/libpng/$(libc)" \
	  --with-tiff="$$builddir/3rdparty/dist/libtiff/$(libc)" \
	  --with-pic --with-rgb --with-targa --with-avienc \
	  --disable-debug \
	  --disable-symbols \
	  --with-msvcrt=$(libc) \
	  --prefix=$(sdk_prefix)
	cd build-shared-release; \
	$(MAKE)
	cd build-shared-release; \
	$(MAKE) install

shared-release: build-shared-release/src/Inventor/Win/$(project)$(major).dll

build-shared-debug/src/Inventor/Win/$(project)$(major)d.dll: 3rdparty $(Project)-$(VERSION)
	@if test -d build-shared-debug; then :; else mkdir build-shared-debug; fi
	builddir=`pwd`; \
	cd build-shared-debug; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(DBGCFLAGS)" \
	  CXXFLAGS="$(DBGCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --with-ungif="$$builddir/3rdparty/dist/libungif/$(libc)d" \
	  --with-jpeg="$$builddir/3rdparty/dist/libjpeg/$(libc)d" \
	  --with-zlib="$$builddir/3rdparty/dist/zlib/$(libc)d" \
	  --with-png="$$builddir/3rdparty/dist/libpng/$(libc)d" \
	  --with-tiff="$$builddir/3rdparty/dist/libtiff/$(libc)d" \
	  --with-pic --with-rgb --with-targa --with-avienc \
	  --enable-debug \
	  --enable-symbols \
	  --with-msvcrt=$(libc)d \
	  --with-alternate=debug \
	  --with-suffix=d \
	  --prefix=$(sdk_prefix)
	cd build-shared-debug; \
	$(MAKE)
	cd build-shared-debug; \
	$(MAKE) install

shared-debug: build-shared-debug/src/Inventor/Win/$(project)$(major)d.dll

build-static-release/src/Inventor/Win/$(project)$(major)d.dll: $(Project)-$(VERSION)
	@if test -d build-static-release; then :; else mkdir build-static-release; fi
	cd build-static-release; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(OPTCFLAGS)" \
	  CXXFLAGS="$(OPTCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --disable-shared \
	  --disable-debug \
	  --disable-symbols \
	  --with-msvcrt=$(libc) \
	  --with-alternate=static \
	  --with-suffix=s \
	  --prefix=$(sdk_prefix)
	cd build-static-release; \
	$(MAKE)
	cd build-static-release; \
	$(MAKE) install

static-release: build-static-release/src/Inventor/Win/$(project)$(major)d.dll

build-static-debug/src/Inventor/Win/$(project)$(major)d.dll: $(Project)-$(VERSION)
	@if test -d build-static-debug; then :; else mkdir build-static-debug; fi
	cd build-static-debug; \
	../$(Project)-$(VERSION)/configure \
	  CFLAGS="$(DBGCFLAGS)" \
	  CXXFLAGS="$(DBGCFLAGS)" \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --disable-shared \
	  --enable-debug \
	  --enable-symbols \
	  --with-msvcrt=$(libc)d \
	  --with-alternate=static-debug \
	  --with-suffix=sd \
	  --prefix=$(sdk_prefix)
	cd build-static-debug; \
	$(MAKE)
	cd build-static-debug; \
	$(MAKE) install

static-debug: build-static-debug/src/Inventor/Win/$(project)$(major)d.dll

buildfiles: build-files/$(project)$(major).dsp

build-files/$(project)$(major).dsp: $(Project)-$(VERSION) 3rdparty
	@if test -d build-files; then :; else mkdir build-files; fi
	@if test -e build-files/$(project)$(major).dsp; then \
	rm build-files/$(project)$(major).*; fi
	builddir=`pwd`; \
	cd build-files; \
	../$(Project)-$(VERSION)/configure \
	  $(CONFIGUREOPTIONS) \
	  $(BUILDOPTIONS) \
	  --with-ungif="$$builddir/3rdparty/dist/libungif/$(libc)" \
	  --with-jpeg="$$builddir/3rdparty/dist/libjpeg/$(libc)" \
	  --with-zlib="$$builddir/3rdparty/dist/zlib/$(libc)" \
	  --with-png="$$builddir/3rdparty/dist/libpng/$(libc)" \
	  --with-tiff="$$builddir/3rdparty/dist/libtiff/$(libc)" \
	  --with-msvcrt=$(libc) \
	  --enable-msvcdsp; \
	$(MAKE) clean && $(MAKE); \
	wbuilddir=`cygpath -w $$builddir | sed -e 's%\\\\%\\\\\\\\%g'`; \
	version=`echo "@VERSION@" | sed -e 's%\.%\\\.%g'`; \
	cp $(project)$(major).dsp $(project)$(major).dsp.orig; \
	cat $(project)$(major).dsp.orig | \
	  sed -e "s%$$wbuilddir%\.\.\\\\\.\.%g" \
	      -e "s%\"\.\\\\include\"%\"\.\"%g" \
	      -e "s%\\\\3rdparty\\\\dist\\\\%\\\\3rdparty\\\\%g" \
	      -e "s%\.\.\\\\source\\\\simage-$$version%\.\.\\\\\.\.\\\\source\\\\simage-$(major)%g" \
	      -e "s%/out:\"%/out:\"\.\.\\\\\.\.\\\\bin\\\\%g" \
	      -e "s%$(project)$(major)d\.dll\"%$(project)$(major)d\.dll\" /pdb:\"\.\.\\\\\.\.\\\\bin\\\\$(project)$(major)d.pdb\" /implib:\"\.\.\\\\\.\.\\\\lib\\\\$(project)$(major)d.lib\"%g" \
	      -e "s%$(project)$(major)\.dll\"%$(project)$(major)\.dll\" /implib:\"\.\.\\\\\.\.\\\\lib\\\\$(project)$(major).lib\"%g" \
	      -e "/\/D \"_DEBUG\"/ s%\\\\md\\\\%\\\\mdd\\\\%g" \
	      -e "/debug/ s%\\\\md\\\\%\\\\mdd\\\\%g" \
	  > $(project)$(major).dsp; \
	rm $(project)$(major).dsp.orig

.PHONY: shared-release shared-debug static-release static-debug buildfiles \
	3rdparty

# **************************************************************************
